package com.baomidou.springwind.pojo;
import com.baomidou.springwind.entity.NetworkNode;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.*;
import org.python.util.PythonInterpreter;
import org.python.core.*;



/**
 * Created by demo on 2017/7/13.
 */
public class Test {
    /**
     * java直接调用python
     * @param args
     */
/*
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile("e:/my_utils.py");
        PyFunction pyFunction = interpreter.get("adder", PyFunction.class); // 第一个参数为期望获得的函数（变量）的名字，第二个参数为期望返回的对象类型
        int a=2010,b=2;
        PyObject pyObject = pyFunction.__call__(new PyInteger(a),new PyInteger(b)); // 调用函数
        System.out.println("anwser="+pyObject.toString());
    }
*/

    /**
     * java调用shell,shell调用python
     * @param args
     * by:www.huanghetaotao.com
     */
    public static void main(String[] args) {
        String  parm1="first";
        String parm2="second";
        String cmd = "cmd /k start E:\\test.bat";// pass
        cmd=cmd+" "+parm1+" "+parm2;
        try {
            Process ps = Runtime.getRuntime().exec(cmd);
            InputStream in = ps.getInputStream();
            int c;
            while ((c = in.read()) != -1) {
                System.out.print(c);// 如果你不需要看输出，这行可以注销掉
            }
            in.close();
            ps.waitFor();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("child thread donn");
    }


    private void callScript(){

    }
    public static LinkedHashMap<NetworkNode, List<NetworkNode>> getMaps(List<DrivingRoute> drivingRoutes) throws Exception {
        Map<NetworkNode, Map<NetworkNode, Long>> nodeMaps = new LinkedHashMap<NetworkNode, Map<NetworkNode, Long>>();
        for (DrivingRoute drivingRoute : drivingRoutes) {
            Map<NetworkNode, Long> relationMap = nodeMaps.get(drivingRoute.node1);
            if (relationMap == null) {
                relationMap = new HashMap<NetworkNode, Long>();
            }
            relationMap.put(drivingRoute.node2, drivingRoute.distance);
            nodeMaps.put(drivingRoute.node1, relationMap);
            relationMap = nodeMaps.get(drivingRoute.node2);
            if (relationMap == null) {
                relationMap = new HashMap<NetworkNode, Long>();
            }
            relationMap.put(drivingRoute.node1, drivingRoute.distance);
            nodeMaps.put(drivingRoute.node2, relationMap);
        }
        Map<NetworkNode, Long> transMap;
        for (Map.Entry<NetworkNode, Map<NetworkNode, Long>> entry : nodeMaps.entrySet()) {
            transMap = new TreeMap<NetworkNode, Long>(new ValueComparator(entry.getValue()));
            transMap.putAll(entry.getValue());
            nodeMaps.put(entry.getKey(), transMap);
        }
      return new GroupBuilder(nodeMaps).build();
    }
    static class ValueComparator implements Comparator<NetworkNode> {

        Map<NetworkNode, Long> map;

        //这里需要将要比较的map集合传进来
        public ValueComparator(Map<NetworkNode, Long> map) {
            this.map = map;
        }

        public int compare(NetworkNode a, NetworkNode b) {
            return map.get(a).intValue() - map.get(b).intValue();
        }
    }
}
